package com.example.chapter;

import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.*;

public class CalculatorActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView cal_result;
    //第一个操作数
    private String firstNum = "";
    //第二个操作数
    private String secondNum = "";
    //操作符
    private String operator = "";
    //结果
    private String result = "";
    //显示文本内容
    private String showText = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculator);

        cal_result = findViewById(R.id.cal_result);

        findViewById(R.id.cal_cencel).setOnClickListener(this);
        findViewById(R.id.cal_divide).setOnClickListener(this);
        findViewById(R.id.cal_multiply).setOnClickListener(this);
        findViewById(R.id.cal_clear).setOnClickListener(this);
        findViewById(R.id.cal_seven).setOnClickListener(this);
        findViewById(R.id.cal_eight).setOnClickListener(this);
        findViewById(R.id.cal_nine).setOnClickListener(this);
        findViewById(R.id.cal_plus).setOnClickListener(this);
        findViewById(R.id.cal_four).setOnClickListener(this);
        findViewById(R.id.cal_five).setOnClickListener(this);
        findViewById(R.id.cal_six).setOnClickListener(this);
        findViewById(R.id.cal_minus).setOnClickListener(this);
        findViewById(R.id.cal_one).setOnClickListener(this);
        findViewById(R.id.cal_two).setOnClickListener(this);
        findViewById(R.id.cal_three).setOnClickListener(this);
        findViewById(R.id.cal_reciprocal).setOnClickListener(this);
        findViewById(R.id.cal_zero).setOnClickListener(this);
        findViewById(R.id.cal_dot).setOnClickListener(this);
        findViewById(R.id.cal_equal).setOnClickListener(this);
        findViewById(R.id.cal_genhao).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        String inputText;

        if (v.getId() == R.id.cal_genhao) {
            inputText = "√";
        } else {
            inputText = ((TextView)v).getText().toString();
        }

        switch (v.getId()) {
            //清楚按钮
            case R.id.cal_clear:
                clear();
                break;
            //取消按钮
            case R.id.cal_cencel:
                break;
            //加减乘除按钮
            case R.id.cal_plus:
            case R.id.cal_minus:
            case R.id.cal_multiply:
            case R.id.cal_divide:
                operator = inputText;
                refreshText(showText + operator);
                break;
            //等号按钮
            case R.id.cal_equal:
                double calculate_result = calculateFour();
                refreshOperate(String.valueOf(calculate_result));
                refreshText(showText + "=" + result);
                break;
            //根号按钮
            case R.id.cal_genhao:
                double sqrt_result = Math.sqrt(Double.parseDouble(firstNum));
                refreshOperate(String.valueOf(sqrt_result));
                refreshText(showText + "√=" + result);
                break;
            //倒数按钮
            case R.id.cal_reciprocal:
                double reciprocal_result = 1.0 / Double.parseDouble(firstNum);
                refreshOperate(String.valueOf(reciprocal_result));
                refreshText(showText + "/=" + result);
                break;
            //其他按钮，数字，小数点
            default:
                if (result.length() > 0 && operator.equals("")) {
                    clear();
                }

                if (operator.equals("")) {
                    firstNum = firstNum +inputText;
                } else {
                    secondNum = secondNum +inputText;
                }

                if (showText.equals("0") && !inputText.equals(".")) {
                    refreshText(inputText);
                } else {
                    refreshText(showText + inputText);
                }
                break;
        }
    }

    private double calculateFour() {
        switch (operator) {
            case "+":
                return Double.parseDouble(firstNum) + Double.parseDouble(secondNum);
            case "-":
                return Double.parseDouble(firstNum) - Double.parseDouble(secondNum);
            case "x":
                return Double.parseDouble(firstNum) * Double.parseDouble(secondNum);
            default:
                return Double.parseDouble(firstNum) / Double.parseDouble(secondNum);
        }
    }

    //清空初始化
    private void clear() {
        refreshOperate("");
        refreshText("");
    }

    //刷新运算结果
    private void refreshOperate(String new_result) {
        result = new_result;
        firstNum = result;
        secondNum = "";
        operator = "";
    }

    //刷新文本显示
    private void refreshText(String text) {
        showText = text;
        cal_result.setText(showText);
    }
}